package com.oepm.web.controller;

import com.oepm.biz.AcademyBiz;
import com.oepm.dao.entity.AcademyPo;
import com.oepm.vo.DataVo;
import com.oepm.biz.TeaDemoFormBiz;
import com.oepm.common.entity.Meta;
import com.oepm.common.entity.Result;
import com.oepm.common.utils.JWTUtil;
import com.oepm.dao.entity.QueryInfo;
import com.oepm.dao.entity.TeacherDemoApplicationPo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin//解决跨域
@RestController
@RequestMapping("/titleUpload")
@Api(tags = "老师申请表")
public class TeaDemoFormController {


    @Autowired
    private TeaDemoFormBiz teaDemoFormBiz;
    @Autowired
    private AcademyBiz acadenyBiz;

    @PostMapping("/insert")
    @ApiOperation("申报项目")
    @RequiresRoles(value = {"4"})
    public Result insertTeaForm(@RequestBody TeacherDemoApplicationPo teaPo,ServletRequest request){
        HttpServletRequest req= (HttpServletRequest) request;
        String token=req.getHeader("Authorization");
        //插入学院
        teaPo.setAcademy(JWTUtil.getAcademy(token));
        System.out.println(JWTUtil.getAcademy(token));
        String level = teaPo.getLevel();
        if (level.equals("重点")){
            teaPo.setAdmin_status("待审核");
        }else{
            teaPo.setAdmin_status("");
        }
        int flag = teaDemoFormBiz.addTeaDemoForm(teaPo);
        Result result;
        if (flag>0){
            result = new Result(Meta.CREATED);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    @GetMapping("/select")
    @ApiOperation("查询已申报项目")
    @RequiresRoles(value = {"4"})
    public Result selectTeaForm(ServletRequest request,QueryInfo queryInfo){
        HttpServletRequest req= (HttpServletRequest) request;
        String token=req.getHeader("Authorization");
        String userId = JWTUtil.getUserId(token);
        DataVo dataVo = teaDemoFormBiz.selectTeaForm(userId, queryInfo);
        Result result = new Result(Meta.SUCCESS);
        result.setData(dataVo);
        return result;
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除未审核，及审核未通过的题目")
    @RequiresRoles(value = {"4"})
    public Result deleteTeaForm(@PathVariable String id){
        System.out.println("==========="+id);
        int flag = teaDemoFormBiz.deleteTeaDemoForm(id);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    @PostMapping("/update")
    @ApiOperation("修改申报项目")
    @RequiresRoles(value = {"4"})
    public Result insertTeaForm(@RequestBody TeacherDemoApplicationPo teaPo){
        int flag = teaDemoFormBiz.updateTeaDemoFrom(teaPo);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    @GetMapping("/majors")
    @RequiresRoles(value = {"4"})
    public Result getMajors(){
        List<AcademyPo> list = acadenyBiz.selectAll();
        Result result;
        if (list != null){
            result = new Result(Meta.SUCCESS);
            result.setData(list);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }
}
